package org.demo.honor.pojo.entity;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.demo.honor.pojo.constants.OrderStatusEnum;

@Entity
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "mobile_order")
public class OrderEntity {

    @Id
    @Column(name = "id")
    private Integer id;
    @Column
    private String customerId;
    @Column
    private String mobileName;
    @Column
    private String mobileId;
    @Column
    private String customerName;
    @Column
    private String customerMobile;
    @Column
    private BigDecimal singleAmount;
    @Column
    private BigDecimal totalAmount;
    @Column
    private Integer count;
    @Column
    private Date salesTime;
    @Column
    private Date paymentTime;
    @Column
    private String comment;
    @Column
    private String paymentId;
    @Column
    @Enumerated(EnumType.STRING)
    private OrderStatusEnum orderStatus;

    @OneToMany(mappedBy = "id", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<OrderMobileEntity> orderMobileEntities;


    public List<OrderMobileEntity> getOrderMobileEntities() {
        if (Objects.isNull(orderMobileEntities)) {
            orderMobileEntities = new ArrayList<>();
        }
        return orderMobileEntities;
    }

    public void setOrderMobiles(List<String> imeis) {
        orderMobileEntities = imeis.stream()
            .map(imei -> new OrderMobileEntity(id, imei))
            .collect(Collectors.toList());
    }


}
